package edu.whu.app;

import edu.whu.aspect.CountAspect;
import edu.whu.aspect.ResponseTimeAspect;
import edu.whu.bean.User;
import edu.whu.exception.RegisterException;
import edu.whu.exception.UnregisterException;
import edu.whu.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class Cal {
    Logger logger = LoggerFactory.getLogger(getClass());
    public void printInfo(UserService service) throws RegisterException, UnregisterException {
        List<User> users = new ArrayList<>();
        users.add(new User(1,"user"));
        users.add(new User(2,"user"));
        users.add(new User(3,"test"));
        users.add(new User(4,"assignment"));
        users.add(new User(5,"name"));
        for (User user : users) {
            service.register(user);
        }
        List<User> users1 = service.selectAll();
        User user1 = service.selectById(2);
        List<User> users2 = service.selectByName("user");
        List<User> users3 = service.selectByName("test");
        for (User user : users) {
            service.unregister(user);
        }

        try {service.register(null);}catch (Exception e){}
        try {service.register(null);}catch (Exception e){}
        try {service.register(null);}catch (Exception e){}

        try {service.unregister(new User(-1,"user"));}catch (Exception e){}
        try {service.unregister(new User(-1,"user"));}catch (Exception e){}
        try {service.unregister(new User(-1,"user"));}catch (Exception e){}
        try {service.unregister(new User(-1,"user"));}catch (Exception e){}

        Set<String> strings = CountAspect.map.keySet();
        for (String str : strings) {
            logger.info("调用次数:" + str + CountAspect.map.get(str));
        }
        strings = ResponseTimeAspect.map.keySet();

        for (String str : strings) {
            List<Long> times = ResponseTimeAspect.map.get(str);
            String info = str + "调用时间(";
            info += "最短时间:" + Collections.min(times) + ",";
            info += "最长时间:" + Collections.max(times) + ",";
            Double sum = 0.0;
            for (Long l : times) {
                sum += l;
            }
            info += "平均时间:" + sum/times.size();
            info += ")";
            logger.info(info);
        }
    }
}
